package com.tx.test.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=UTF-8");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String validCode = request.getParameter("validCode");
		HttpSession session = request.getSession();
		System.out.println(request.getSession().getAttribute("vCode"));
		if ("wanghy".equals(userName) && "wanghy".equals(password) && session.getAttribute("vCode").equals(validCode.toLowerCase())) {
			response.getWriter().println("{success:true,url:'jsp/home.jsp'}");
		} else {
			response.getWriter().println("{success:false,errors:[");
			if (!session.getAttribute("vCode").equals(validCode.toLowerCase())) {
				response.getWriter().println("{id:'validCode',msg:'验证码错误'},");
			}
			if (!"wanghy".equals(userName)) {
				response.getWriter().println("{id:'userName',msg:'账号错误'},");
			}
			if (!"wanghy".equals(password)) {
				response.getWriter().println("{id:'password',msg:'密码错误'}");
			}
			response.getWriter().println("]}");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
